using MultiplayerServerConnectionHandlers;
using UnityEngine;

public class RacketControlDataSender : MonoBehaviour
{
	public bool sendDataContinuously;

	public float secondsBetweenUpdates = 0.05f;

	private float currentTime;

	private void OnEnable()
	{
		sendDataContinuously = false;
	}

	public void SendDataNow(bool isTouching = false)
	{
		ControllerData controllerData = new ControllerData();
		controllerData.attitude = Input.gyro.attitude;
		controllerData.gravity = Input.gyro.gravity;
		controllerData.rotationRate = Input.gyro.rotationRate;
		controllerData.rotationRateUnbiased = Input.gyro.rotationRateUnbiased;
		controllerData.screenOrientation = (int)Screen.orientation;
		controllerData.updateInterval = Input.gyro.updateInterval;
		controllerData.userAcceleration = Input.gyro.userAcceleration;
		controllerData.headingAccuracy = Input.compass.headingAccuracy;
		controllerData.magneticHeading = Input.compass.magneticHeading;
		controllerData.rawMagneticVector = Input.compass.rawVector;
		controllerData.magneticTimestamp = Input.compass.timestamp;
		controllerData.trueHeading = Input.compass.trueHeading;
		controllerData.isUserTouchingSurface = isTouching;
		controllerData.touchPosition = ((!isTouching) ? Vector3.zero : Input.mousePosition);
		Debug.Log("RacketControlDataSender - sending message");
		Debug.Log(string.Concat("msg.rotationRate: ", Input.gyro.rotationRate, ", msg.rotationRateUnbiased: ", Input.gyro.rotationRateUnbiased));
		foreach (MessageHandler handler in BehaviourSingleton<GGUDPNetworkServer>.instance.handlers)
		{
			ConnectionHandler connectionHandler = handler as ConnectionHandler;
			if (connectionHandler != null && connectionHandler.isActive)
			{
				connectionHandler.Send(controllerData);
			}
		}
	}

	private void Update()
	{
		if (sendDataContinuously)
		{
			currentTime += RealTime.deltaTime;
			if (currentTime >= secondsBetweenUpdates)
			{
				currentTime = 0f;
				SendDataNow();
			}
		}
	}
}
